<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="留言内容" prop="messageContent">
        <el-input
          v-model="dataForm.messageContent"
          placeholder="留言内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="留言姓名" prop="messageName">
        <el-input
          v-model="dataForm.messageName"
          placeholder="留言姓名"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        messageContent: "",
        messageName: "",
        createTime: "",
        updateTime: "",
        isDelete: "",
      },
      dataRule: {
        messageContent: [
          { required: true, message: "留言内容不能为空", trigger: "blur" },
        ],
        messageName: [
          { required: true, message: "留言姓名不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: false, message: "留言时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: false, message: "更新时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/message/message/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.messageContent = data.message.messageContent;
              this.dataForm.messageName = data.message.messageName;
              this.dataForm.createTime = data.message.createTime;
              this.dataForm.updateTime = data.message.updateTime;
              this.dataForm.isDelete = data.message.isDelete;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/message/message/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              messageContent: this.dataForm.messageContent,
              messageName: this.dataForm.messageName,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              isDelete: this.dataForm.isDelete,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
